//
//  ControlFlowController.swift
//  HHSwift
//
//  Created by qk-huahong on 2019/6/20.
//  Copyright © 2019 huahong. All rights reserved.
//

import UIKit

class ControlFlowController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "控制流"
     
       //MARK:for循环
//       forLoop()
        
        //MARK:for循环
//        whileLoop()
//        repeatwhileLoop()
        
        //MARK:swith
         swithFunc()
        
//        for case let i in 1...10 where i % 3 == 0{
//            print("for case where \(i)")
//        }
        
        for  i in 1...10 where i % 3 == 0{
            print("for case where \(i)")
        }
        
    }
    
    
    //MARK:for循环
    func forLoop() {
        //闭区间
        for _ in 0...10 {
            //            print("i:",i)
        }
        
        //开区间
        for _ in 0..<10 {
            //            print("j:",j)
        }
        
        //倒序
        for _ in (0...10).reversed() {
            //           print("i:",i)
        }
        
        //开区间
        for i in stride(from: 0, to: 10, by: 2) {
            print("i:",i)
        }
        
        //闭区间
        for j in stride(from: 0, through: 10, by: 2) {
            print("j:",j)
        }
    }
    
    
    //MARK:while循环
    func whileLoop() {
        var a = 10
        while a > 0 {
            print("while:\(a)")
            
            a -= 1
        }
        
        
        repeat{
          print("repeat while:\(a)")
        }while (a <= 0)
        
    }
    
    func repeatwhileLoop() {
        var a = 10
        repeat{
            
            //结束本次循环，继续下次循环
            if (a == 6){
                 a -= 1
              continue
            }
            
            //结束整个循环
            if (a == 3){
                break
            }
            
            print("repeat while:\(a)")
            a -= 1
            
        }while (a > 0)
        
    }
    
    
    //MARK:swith
    func swithFunc() {
        
        let x = 2.0,y = true,score = 90
        
        //判断多个条件
        switch x {
        case 2.0,3.0:
            print("2.0 or 3.0")
        default:
             print("not 2.0 or 3.0")
        }
        
       //Bool
        switch y {
        case true:
            print("true")
        default:
             print("false")
        }
        
        //区间
        switch score {
        case 0..<60:
            print("不及格")
        case 60..<80:
            print("及格")
        case 80...100:
            print("优秀")
        default:
            print("error")
        }
        
        //向量（元组）
        let v = (1,0)
        switch v {
        case (0,0):
            print(v)
            fallthrough
        case (0,_):
            print(v)
        case let(x,y) where x != y:
            print("x: y:",x,y)
        case (-2..<2,-1...1):
            print(v)
        default:
             print("error")
        }
       
        // 注意：fallthrough 并不会判断下一个case（或者default）是否符合switch的条件，而是直接跳到下一个case（或者default）。
        // 所以，首先，fallthrough不能跳转到一个有逻辑的case中
        // 其次，不要用switch和fallthrough组合复杂的判断逻辑
        
        if case 80...100 = score,score >= 90 && score < 95 {
            print("优秀:",score)
        }
    }
    
    //guard
    func buy( money: Int , price: Int , capacity: Int , volume: Int){
        
        guard money >= price else{
            print("Not enough money")
            return
        }
        
        guard capacity >= volume else{
            print("Not enough capacity")
            return
        }
        
        print("\(money-price) Yuan left.")
        print("\(capacity-volume) cubic meters left")
    }
}
